package jee.boot.module.mail.queue;

import jee.boot.common.utils.JsonMapper;
import jee.boot.entity.mail.Mail;
import jee.boot.module.mail.sender.MailTaskHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;


/**
 * 邮件消费者
 * 
 * @author zhilei.wang
 *
 */
@Component
public class MailConsumer {
	private static final Logger log = LoggerFactory.getLogger(MailProducer.class);
	@Autowired
	private MailTaskHandler mailSender;//邮件发送接口
	@Value("${spring.mail.channel:mail.queue}")
	private String channel;

	/**
	 * 从消息队列中读取邮件信息,并发送
	 * @param json
	 * @throws Exception
	 */
	@JmsListener(destination = "${spring.mail.channel:mail.queue}")
	public void receiveQueue(String json) throws Exception {
		log.debug("mq:receive["+channel+"]" + json);
		Mail mail = (Mail) JsonMapper.fromJsonString(json, Mail.class);
		try {
			mailSender.send(mail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}